package com.xinke;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.boot.web.context.WebServerPortFileWriter;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Description:
 *
 * @Author yzx
 * @Create 2025/3/15 23:41
 * @Version 1.0
 */

@SpringBootApplication
@Slf4j
public class XinkeSystemApplication {
    public static void main(String[] args) {
        long startTimeMillis = System.currentTimeMillis();
        log.info("开始初始化系统...");
        SpringApplication app = new SpringApplication(XinkeSystemApplication.class);
        app.addListeners(new ApplicationPidFileWriter(), new WebServerPortFileWriter());
        Environment env = app.run(args).getEnvironment();
        logApplicationStartup(env);
        long endTimeMillis = System.currentTimeMillis();
        log.info("系统初始化成功,耗时：{}s", (endTimeMillis - startTimeMillis) / 1000);
    }

    private static void logApplicationStartup(Environment env) {
        String protocol = "http";
        if (env.getProperty("server.ssl.key-store") != null) {
            protocol = "https";
        }
        String serverPort = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        if (StringUtils.isEmpty(contextPath)) {
            contextPath = "/";
        }
        String hostAddress = "localhost";
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            log.warn("The host name could not be determined, using `localhost` as fallback");
        }
        // @formatter:off
        log.info("\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\t{}://localhost:{}{}\n\t"
                        + "External: \t{}://{}:{}{}\n\t"
                        + "Profile(s): \t{}\n----------------------------------------------------------",
                env.getProperty("spring.application.name"), protocol, serverPort, contextPath, protocol, hostAddress,
                serverPort, contextPath, env.getActiveProfiles());
    }
}
